package com.generator.purchaseManage.manufacturerManage.controller;


import com.generator.statics.manufacturerPurchaseStatics.pojo.City1;
import com.generator.statics.manufacturerPurchaseStatics.pojo.Province1;
import com.generator.purchaseManage.manufacturerManage.pojo.*;
import com.generator.purchaseManage.manufacturerManage.service.manufacturerManageService;
import com.generator.util.PageModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.Objects;

@Controller
@RequestMapping("/manufacturerManage")
public class manufacturerManageAction {

    @Autowired
    private manufacturerManageService manufacturerManageServiceFun;

    public manufacturerManageService getManufacturerManageServiceFun() {
        return manufacturerManageServiceFun;
    }

    public void setManufacturerManageServiceFun(manufacturerManageService manufacturerManageServiceFun) {
        this.manufacturerManageServiceFun = manufacturerManageServiceFun;
    }

    // 查询所有厂商管理信息
    @RequestMapping("/getAllManuManagement")
    public String allPurchase(Model model, CFirmManu cPurchase, PageModel<CFirmManuConn> pageModel) {

        PageModel<CFirmManuConn> manuManagementPage = manufacturerManageServiceFun.getAllManuManagement(cPurchase, pageModel);

        model.addAttribute("manuManagementPage", manuManagementPage);

        return "forward:/manufacturerManage/jumpPage";
    }

    // 跳转到purchaseStatis页面
    @RequestMapping("/jumpPage")
    public String jumpPage(Model model) {
        return "purchase/manufacturer/manufacturerList";
    }

    // 查询单个厂商管理信息
    @RequestMapping("/getOneManuManagement/{id}")
    public String getOneManuManagement(@PathVariable("id") long id, Model model) {

        CFirmManuConn conn = new CFirmManuConn();
        conn.setFirmId(id);

        CFirmManuConn oneManuManagement = manufacturerManageServiceFun.getOneManuManagement(conn);

        model.addAttribute("oneManuManagement", oneManuManagement);
        return "purchase/manufacturer/manufacturerView";

    }

    // 厂商管理信息列表（分页）
    @RequestMapping("/facturerList")
    public String userList(Model model, CFirmManu user, PageModel<CFirmManuConn> pageModel){
        PageModel<CFirmManuConn> userList = manufacturerManageServiceFun.getAllManuManagement(user,pageModel);
        model.addAttribute("manuManagementPage",userList);
        return "forward:/manufacturerManage/jumpPage";
    }



    // 修改回显厂商管理信息
    @RequestMapping("/getUpdateManuManagement/{id}")
    public String getUpdateManuManagement(@PathVariable("id") long id, Model model) {

        CFirmManuConn conn = new CFirmManuConn();
        conn.setFirmId(id);

        CFirmManuConn oneManuManagement = manufacturerManageServiceFun.getOneManuManagement(conn);

        System.out.println("oneManuManagement: " + oneManuManagement);

        model.addAttribute("oneManuManagement", oneManuManagement);
        return "purchase/manufacturer/manufacturerUpdate";

    }


    // 修改厂商管理信息
    @RequestMapping("/updateManuManagement")
    public String updateManuManagement(@Param("id") long id,@Param("firmName") String firmName, @Param("firmFounder") String firmFounder,
                                       @Param("firmTel") String firmTel, @Param("firmAddress") String firmAddress, @Param("sheng") int sheng,
                                       @Param("shi") int shi, @Param("firmContent") String firmContent, @Param("dfselect") int dfselect, Model model) {


        CFirmManuConn conn = new CFirmManuConn();
        conn.setFirmId(id);
        conn.setFirmName(firmName);
        conn.setFirmFounder(firmFounder);
        conn.setFirmTel(firmTel);
        conn.setFirmAddress(firmAddress);
        conn.setSheng(sheng);
        conn.setShi(shi);
        conn.setFirmContent(firmContent);
        conn.setDfselect(dfselect);

        int i = manufacturerManageServiceFun.updateManuManagement(conn);
        String s = "redirect:/manufacturerManage/getUpdateManuManagement/" + id;

        if (i > 0){
            return "redirect:/manufacturerManage/getAllManuManagement";
        }else{
            return s;
        }

    }

    // 注销厂商管理信息
    @RequestMapping("/deleteManuManagement/{id}")
    public String deleteManuManagement(@PathVariable("id") String id){
        int i = manufacturerManageServiceFun.deleteManuManagement(id);
        return "redirect:/manufacturerManage/getAllManuManagement";
    }


    //  添加厂商信息
    @RequestMapping("/addManuManagement")
    public String addManuManagement(@Param("firmName") String firmName, @Param("firmFounder") String firmFounder,
                          @Param("firmTel") String firmTel, @Param("firmAddress") String firmAddress,@Param("sheng") int sheng,
                          @Param("shi") int shi, @Param("firmContent") String firmContent, @Param("dfselect") int dfselect){

        CFirmManuConn conn = new CFirmManuConn();
        Date date = new Date();
        long time = new Date().getTime();
        System.out.println("time: " + time);

        conn.setFirmId(time);
        conn.setFirmCity("10");
        conn.setCreateId("1");
        conn.setCreateTime(date);
        conn.setFirmName(firmName);
        conn.setFirmFounder(firmFounder);
        conn.setFirmTel(firmTel);
        conn.setFirmAddress(firmAddress);
        conn.setFirmContent(firmContent);
        conn.setDfselect(dfselect);

        int i = manufacturerManageServiceFun.addManuManagement(conn);

        if (i > 0){
            return "redirect:/manufacturerManage/getAllManuManagement";
        }else{
            return "forward:/manufacturerManage/addManuManagement";
        }

    }

    // 厂商信息分类查询
    @RequestMapping("/vagueManufacturerSelect")
    public String vagueManufacturerSelect(@Param("firmName") String firmName, @Param("firmFounder") String firmFounder,
                                          @Param("status") Integer status, @Param("sheng") int sheng, @Param("shi") int shi, Model model, PageModel<CFirmManuConn> pageModel) {

        CFirmManuConn conn = new CFirmManuConn();

        if (Objects.equals(firmName, "%") || Objects.equals(firmName, "_")){
            conn.setFirmName(")(@#~【】");
        }else {
            conn.setFirmName(firmName);
        }

        if (Objects.equals(firmFounder, "%") || Objects.equals(firmFounder, "_")){
            conn.setFirmFounder(")(@#~【】");
        }else {
            conn.setFirmFounder(firmFounder);
        }

        Province1 province = new Province1();
        City1 city = new City1();
        if (Objects.equals(shi, "0")){
            province.setId(sheng);
            String provinces = manufacturerManageServiceFun.queryProvince(province);
            conn.setFirmAddress(provinces);
        }else{
            city.setId(shi);
            String city1 = manufacturerManageServiceFun.queryCity(city);
            System.out.println("city1: " + city1);
            conn.setFirmAddress(city1);
        }

        if (status == 1 || status == 0){
            conn.setStatus(status);
        }

        PageModel<CFirmManuConn> purchasePage = manufacturerManageServiceFun.vagueManufacturerSelect(conn, pageModel);

        model.addAttribute("manuManagementPage", purchasePage);

        return "forward:/manufacturerManage/jumpPage";
    }



}
